﻿using System.Collections.Generic;
using DataAccess.Entities;
using NHibernate;
using NHibernate.Criterion;

namespace DataAccess.Repositories
{
    public class ProfileRepository : IRepository<Profile>
    {
        #region IRepository<Profile> Members

        void IRepository<Profile>.Save(Profile entity)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(entity);
                    transaction.Commit();
                }
            }
        }

        void IRepository<Profile>.Update(Profile entity)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Update(entity);
                    transaction.Commit();
                }
            }
        }

        void IRepository<Profile>.Delete(int id)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Delete(id);
                    transaction.Commit();
                }
            }
        }

        Profile IRepository<Profile>.GetById(int id)
        {
            using (ISession session = NHibernateHelper.OpenSession())
                return session.CreateCriteria<Profile>().Add(Restrictions.Eq("Id", id)).UniqueResult<Profile>();
        }

        IList<Profile> IRepository<Profile>.GetAll()
        {
            using (ISession session = NHibernateHelper.OpenSession())
                return session.CreateCriteria<Profile>().List<Profile>();
        }

        public IList<Profile> Find(string searchText, int maxResults)
        {
            throw new System.NotImplementedException();
        }

        #endregion
    }
}
